/*
 * ClientApp.java
 */

package client;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import java.io.* ;
import java.net.* ;
import java.util.* ;

/**
 * The main class of the application.
 */
public class ClientApp extends SingleFrameApplication {
    public static ServerSocket socket;
  // public static ServerComm serverComm;
    public static ClientView clientView;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        
        clientView = new ClientView(this);
        show(clientView);
        
     //   clientView.listen_tcp();
       
    }
    
    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of ClientApp
     */
    public static ClientApp getApplication() {
        return Application.getInstance(ClientApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        
            launch(ClientApp.class, args);
            listen_tcp();
        
    }
    public static void listen_tcp(){
     //   String[] message = {""};
        int port = 4000;
        try {
            // Establish the listen socket.
            socket = new ServerSocket(port);
        } catch (IOException ex) {
            Logger.getLogger(ClientApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {

            // Process requests in an infinite loop.
            while (true) {
                System.out.println("I am in the mail while loop, listening the tcp connection");
                // Listen for a TCP connection request.
                Socket connection = socket.accept();
                // Construct an object to process the HTTP request message.
                tcp_request request = new tcp_request(connection, clientView);
                // Create a new thread to process the request.
                Thread thread = new Thread(request);
                // Start the thread.
                thread.start();
              //  clientView.process_message(message);
            }
        } catch (Exception ex) {
            Logger.getLogger(ClientApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
    
